<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('积分商品列表')"/>
    <th:block th:include="include :: bootstrap-suggest-js"/>
</head>

<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="select-credit-product-form">
                <input type="hidden" name="isEnable" value="1">
                <input type="hidden" name="memberIdStr" id="memberIdStr" th:value="${memberIdStr}">
                <div class="select-list">
                    <ul>
                        <li>
                            <p style="width: 80px">商品名称： </p>
                            <input type="text" name="productName" value=""/>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="formReset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>
        <div class="col-sm-12 search-collapse">
            <p style="width: 110px">选择短信模板： </p>
            <div class="input-group">
                <input type="text" id="templateDesc" class="form-control"/>
                <input type="hidden" name="templateCode" class="form-control"/>
                <div class="input-group-btn">
                    <button type="button" class="btn btn-white dropdown-toggle" data-toggle="dropdown">
                        <span class="caret"></span>
                    </button>
                    <ul class="dropdown-menu dropdown-menu-right" role="menu">
                    </ul>
                </div>
            </div>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: jasny-bootstrap-js"/>
<th:block th:include="include :: bootstrap-suggest-js"/>
<script th:inline="javascript">
    const productTypeDict = [[${@dict.getType('product_type')}]];
    const prefix = ctx + "back/shopProduct";
    $(function () {
        var options = {
            url: prefix + "/searchCreditList",
            sortName: "createTime desc,orderNum",
            sortOrder: "asc",
            modalName: "积分商城商品",
            paginationShowPageGo: true,
            clickToSelect: true,
            columns: [
                {
                    checkbox: true
                },
                {
                    field: 'id',
                    title: '站点商品ID',
                    visible: false
                },
                {
                    field: 'productCategory',
                    title: '商品分类',
                    sortable: true
                },
                {
                    field: 'productType',
                    title: '商品类型',
                    sortable: true,
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(productTypeDict, value);
                    }
                },
                {
                    field: 'productName',
                    title: '商品名称',
                    sortable: true
                },
                {
                    field: 'coverUrlThumb',
                    title: '封面图',
                    formatter: function (value, row, index) {
                        var imgHtml = "<img class='img-circle img-sm' data-height='%s' data-width='%s' data-target='%s'  style='border-radius: 0;' src='%s' data-src='%s'/>";
                        return $.common.sprintf(imgHtml, 400, 'auto', 'self', value, row.coverUrl);
                    }
                },
                {
                    field: 'productPrice',
                    title: '兑换积分',
                    sortable: true,
                },
                {
                    field: 'sale',
                    title: '已兑换数量',
                    sortable: true
                },
                {
                    field: 'stock',
                    title: '库存',
                    sortable: true,
                },
                {
                    field: 'createTime',
                    title: '创建时间',
                    sortable: true
                }]
        };
        $.table.init(options);

        $("#templateDesc").bsSuggest({
            indexId: 1,
            indexKey: 0,
            keyField: "templateCode",
            hideOnSelect: true,
            emptyTip: '<b>没有找到可用的短信模板</b>',
            effectiveFields: ["templateCode", "templateName", "templateContent"],
            effectiveFieldsAlias: {
                templateCode: "模板标识",
                templateName: "模板名称",
                templateContent: "模板内容",
            },
            data: {
                'value': (function () {
                    var list = '';    // 先定义个值，方便下面赋值
                    $.ajax({
                        type: 'post',
                        url: ctx + "back/AliYunSms/searchTemplateList",
                        data: {
                            pageSize: 1000,
                            pageNum: 1,
                        },
                        dataType: 'json',
                        async: false,
                        success: function (result) {
                            if (result.code == web_status.SUCCESS) {
                                list = result.rows;
                            } else {
                                $.modal.alert(result.msg);
                            }
                        }
                    });
                    return list;
                })()
            }
        }).on('onSetSelectValue', function (e, keyword, data) {
            $('#templateDesc').val(data.templateContent);
            $('input[name="templateCode"]').val(data.templateCode);
        });

    });

    function queryParams(params) {
        var search = $.table.queryParams(params);
        search.memberIdStr = $('#memberIdStr').val();
        return search;
    }

    /* 选择商品-提交 */
    function submitHandler() {
        var rows = $.table.selectFirstColumns();
        if (rows.length == 0) {
            $.modal.alertWarning("请至少选择一张优惠券赠送");
            return;
        }
        var data = {
            "memberIdStr": $('#memberIdStr').val(),
            "productIdStr": rows.join(),
            "templateCode": $('input[name="templateCode"]').val()
        };
        $.operate.save(prefix + "/sendToMember", data);
    }
</script>
</body>
</html>